# encoding: utf-8
import pymongo
import random
import json

HOST = "127.0.0.1"
PORT = 27017

# client = pymongo.MongoClient("mongodb://rwlog:RWlog@127.0.0.1:27017")
client = pymongo.MongoClient("mongodb://dmteam:dmteam@192.168.30.251")
# client = pymongo.MongoClient(host=HOST, port=PORT)

def simpleTest():
    NewsDB = client.test

    NewsDB.user.drop()
    #删除集合user
    NewsDB.user.save({'id':1,'name':'kaka','sex':'male'})
    for id in range(2,10):
        name = random.choice(['steve','koby','owen','tody','rony'])
        sex = random.choice(['male','female'])
        NewsDB.user.insert({'id':id,'name':name,'sex':sex})
    #通过循环插入一组数据
    content = NewsDB.user.find()
    #打印所有数据
    for i in content:
        print i

def unbantuTest():
    db = client.NewsDB
    content = db.scrapy_items.find({},{"news_title":1,"_id":0})
    result = set()
    for i in content:
        print i["news_title"]
        result.add(i["news_title"])
    print len(result)


def jsonTest():
    s = '[{"name":"niaochao","point":{"lat":"39.990","lng":"116.397"},"desc":"aoyunhuizhuchangdi"},{"name":"beidapingpangqiuguan","point":{"lat":"39.988","lng":"116.315"},"desc":"pingpangqiubisaichangdi"},{"name":"beijinggongrentiyuchang","point":{"lat":"39.930","lng":"116.446"},"desc":"zuqiubisaichangdi"}]'
    s2 = "[{u'news_title': u'\u7279\u65af\u62c9\u7b2c\u4e8c\u5b63\u5ea6\u4ea7\u80fd\u63d0\u5347 \u4f46\u4ea4\u4ed8\u91cf\u4ecd\u4f4e\u4e8e\u9884\u671f_\u7f51\u6613\u79d1\u6280'}]"
    locations = json.loads(s)
    print str(len(locations))
    for location in locations:
        print location["name"]
        print location["point"]["lat"]

    tests = json.dumps(s2)
    tt = json.loads(tests)
    print tests
    for t in tt:
        print t["news_title"]

if __name__ == "__main__":
    unbantuTest()